//
//  MainTabBarController.m
//  梦香菜谱
//
//  Created by imac on 15/9/28.
//  Copyright (c) 2015年 imac. All rights reserved.
//

#import "MainTabBarController.h"


@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    //隐藏系统的TabBar
    self.tabBar.hidden = YES;
    //创建定自定义的TabBar
    [self _createTabBar];
}


//创建自定义的TabBar
- (void)_createTabBar
{
    
    //未选中状态图片
    NSArray *imageNames = @[@"首页1",
                            @"列表1",
                            @"个人1",
                            @"更多1",
                            ];
    //选中状态图片
    NSArray *selectedImageNames = @[
                                    @"首页2",
                                    @"列表2",
                                    @"个人2",
                                    @"更多2"
                                    ];
    //标签栏标题
    NSArray *titles = @[
                        @"首页",
                        @"产品列表",
                        @"我的",
                        @"更多"
                        ];
    //创建整个TabBar
    _tabBarView = [[UIImageView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-49, SCREEN_WIDTH, 49)];
    self.tabBarView.backgroundColor = [UIColor whiteColor];
    _tabBarView.userInteractionEnabled = YES;
    _tabBarView.contentMode = UIViewContentModeScaleToFill;
    // 添加分隔线
    UIView *separateLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    separateLine.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [_tabBarView addSubview:separateLine];
    [self.view addSubview:_tabBarView];
    
    //创建tabBar按钮
    CGFloat buttonWidth = SCREEN_WIDTH / 4;
    for (int i = 0; i < 4; i++) {
        //未选中图片
        UIImage *buttonImage = [UIImage imageNamed:imageNames[i]];
        //选中图片
        UIImage *selectedImage = [UIImage imageNamed:selectedImageNames[i]];
        TabBarButton *button = [[TabBarButton alloc] initWithFrame:CGRectMake(i * buttonWidth, 0, buttonWidth, 49) Image:buttonImage SelectedImage:selectedImage Title:titles[i]];
        button.tag = i;
        if (i == 0) {
            //刚开始设置第一个按钮为选中按钮
            button.isSelectedButton = YES;
            self.selectedButton = button;
        }
        [button addTarget:self action:@selector(_buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBarView addSubview:button];
    }
}


//tabBar按钮点击事件
- (void)_buttonAction:(TabBarButton *)button
{
    //切换视图控制器
    self.selectedIndex = button.tag;
    //上一次选中的按钮状态改为no
    self.selectedButton.isSelectedButton = NO;
    //当前被点击的按钮状态改为yes
    button.isSelectedButton = YES;
    //记录当前选中按钮
    self.selectedButton = button;    
}

#pragma mark
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
